using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Timers;
using System.Windows.Forms;
using System.IO;

namespace ProcessInfoManager
{
    public class ProSysDataProducer
    {
        private Dictionary<string, ProcessInfo> m_processMap;
        private Process[] m_pro;
        private string m_lastProcess;
        private string currentForegroundWindow;

        public string ForegroundWindow
        {
            get { return currentForegroundWindow; }
        }

        public ProSysDataProducer()
        {
            m_processMap = new Dictionary<string, ProcessInfo>();
            m_pro = Process.GetProcesses(Process.GetCurrentProcess().MachineName);

            foreach (Process p in m_pro)
            {
                if (!m_processMap.ContainsKey(p.ProcessName))
                    m_processMap.Add(p.ProcessName, new ProcessInfo());
            }
        }

        //public ProcessInfo Refresh()
        //{
        //    return null;
        //}

        public Dictionary<string, ProcessInfo> Refresh()
        {
            try
            {

                IntPtr fWin = Api.GetForegroundWindow();
                int processID = 0;
                Api.GetWindowThreadProcessId(fWin, ref processID);
                Process p = Process.GetProcessById((int)processID);

                if (m_lastProcess == p.ProcessName || m_lastProcess == "")
                {
                    m_processMap[p.ProcessName].lastTime = new TimeSpan(DateTime.Now.Ticks - m_processMap[p.ProcessName].lastUsingTime.Ticks + m_processMap[p.ProcessName].lastTime.Ticks);
                    m_processMap[p.ProcessName].lastUsingTime = DateTime.Now;
                }
                else
                {
                    m_processMap[p.ProcessName].lastUsingTime = DateTime.Now;
                    m_lastProcess = p.ProcessName;
                }

                currentForegroundWindow = p.ProcessName;
                return m_processMap;
                //Console.WriteLine(p.ProcessName + " : " + processMap[p.ProcessName].lastTime);
            }
            catch
            {
                return null;
                //Console.WriteLine("{0}\t{1}", DateTime.Now.ToString(), ee.Message);
            }
        }
    }

    public class ProcessInfo
    {
        private TimeSpan m_lastTime = new TimeSpan();
        private DateTime m_lastUsingTime = new DateTime(DateTime.Now.Ticks);



        public TimeSpan lastTime
        {
            get { return m_lastTime; }
            set { m_lastTime = value; }
        }

        public DateTime lastUsingTime
        {
            get { return m_lastUsingTime; }
            set { m_lastUsingTime = value; }
        }
    }

    //interface IProcessInfoDisplayer
    //{

    //}

    public class ConsoleProcessDisplayer
    {
        private ProSysDataProducer m_SystemInfo;
        private Dictionary<string, ProcessInfo> displayData;
        
        public ConsoleProcessDisplayer(ProSysDataProducer p)
        {            
            m_SystemInfo = p;
        }


        public void Display()
        {
            displayData = m_SystemInfo.Refresh();
            Console.WriteLine(m_SystemInfo.ForegroundWindow + " : " + displayData[m_SystemInfo.ForegroundWindow].lastTime);
        }
    }

    public class TextBoxProcessDisplayer
    {
        private ProSysDataProducer m_SystemInfo;
        private Dictionary<string, ProcessInfo> displayData;
        private TextBox displayer;
        private List<string> beenDisplayedProcess = new List<string>();
        private string context = "";

        public TextBoxProcessDisplayer(ProSysDataProducer p,TextBox t)
        {
            displayer = t;
            m_SystemInfo = p;
        }

        public void Display()
        {
            displayData = m_SystemInfo.Refresh();
            if(!beenDisplayedProcess.Contains(m_SystemInfo.ForegroundWindow))
                beenDisplayedProcess.Add(m_SystemInfo.ForegroundWindow);

            context = "";
            foreach (string s in beenDisplayedProcess)
            {
                context = context
                    + s 
                    + "\t"
                    + displayData[s].lastTime
                    +"\r\n";
            }

            displayer.Text = context;
        }

        public void OutputToFile(string path)
        {
            TextWriter tw = new StreamWriter(path);
            tw.Write(context);
            tw.Close();
        }
    }
}
